<?php

namespace app\api\controller;

use app\common\controller\Base;
use think\facade\Env;
/**
 * 文件上传
 *
 * @author LCY <409028735@qq.com>
 */
class Upload extends Base
{
    /**
     * 上传图片无水印
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/29 19:10
     * Note:
     */
    public function image()
    {
        $file = $this->request->file('file');
        $base = Env::get('root_path') . 'public/uploads/image';
        if ($file) {
            $res = $file->move($base);
            if ($res) {
                
                $path = $res->getSaveName();

                $url = '/uploads/image/' . str_replace('\\', '/', $path); //线上访问地址

                return json([
                    'msg' => 'ok',
                    'data' => ['url' => $this->request->domain() . $url],
                    'code' => 1,
                ]);
            } else {
                return json([
                    'code' => 0,
                    'msg' => $file->getError(),
                ]);
            }
        } else {
            return json([
                'code' => 0,
                'msg' => '上传失败',
            ]);
        }
    }

}
